package com.intelligent.ispc.foundation.repository.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.repository.core.RepositoryMetadata;

import javax.persistence.EntityManager;
import java.io.Serializable;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Created by Peter on 15/11/16.
 */
public class BaseRepositoryFactory extends JpaRepositoryFactory {

    public BaseRepositoryFactory(EntityManager entityManager) {
        super(entityManager);
    }

    @SuppressWarnings("unchecked")
    protected JpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata, EntityManager em) {
        JpaEntityInformation<Object, Serializable> entityMetadata = mock(JpaEntityInformation.class);
        when(entityMetadata.getJavaType()).thenReturn((Class<Object>) metadata.getDomainType());
        return new BaseDao<Object, Serializable>(entityMetadata, em);
    }

    @Override
    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
        return BaseDao.class;
    }
}
